<template>
  <div>

    <el-button type="primary" @click="handleEdit('')">增加</el-button>
    <el-table
      :data="list"
      border
      style="padding-left: :50;min-width: 100%;">
      <el-table-column
        fixed
        label="操作"
        min-width="10%">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="计量单位"
        min-width="50%"/>
    </el-table>

    <el-pagination
      :current-page="currentPage"
      :page-sizes="[10,20,30]"
      :page-size="10"
      :total="total"
      layout="total,sizes,prev,pager,next,jumper"
      @szie-change="fetchData"
      @current-change="fetchData"/>

    <el-dialog
      :visible.sync="dialogVisible"
      title="编辑信息"
      width="30%">
      <el-form label-width="100PX">
        <el-form-item label="单位">
          <el-input v-model="productUnit.name"/>
        </el-form-item>
      </el-form>
      <span/>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import productUnitApi from '@/api/productUnit'
export default{
  data() {
    return {
      list: [],
      total: 0,			// 总记录数
      currentPage: 1,		// 当前页
      pageSize: 10,		// 每页大小
      searchMap: {},		// 查询条件D
      dialogVisible: false, // 对话框是否可见
      id: '',
      productUnit: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      productUnitApi.findAll(this.currentPage, this.pageSize).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    searchData() {
      productUnitApi.search(this.searchMap, this.currentPage, this.pageSize).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    handleDelete(id) {
      productUnitApi.deleteById(id).then(response => {
        this.$message({
          message: response.message,
          type: 'success'
        })
        if (response.flag) {
          this.fetchData()
        }
      })
    },
    handleSave() {
      productUnitApi.update(this.id, this.productUnit).then(response => {
        this.$message({
          message: response.message,
          type: 'success'
        })
        if (response.flag) {
          this.fetchData()
        }
      })
      this.dialogVisible = false
    },
    handleEdit(id) {
      this.dialogVisible = true
      this.id = id
      if (id === '' || id === null) {
        this.productUnit = {}
      } else {
        productUnitApi.findById(this.id).then(response => {
          this.productUnit = response.data
        })
      }
    }
  }
}
</script>

<style>
</style>
